using Microsoft.AspNetCore.Mvc;
using UniversalAdmin.Application.Dtos.Menu;
using UniversalAdmin.Application.Services.Interfaces;

namespace UniversalAdmin.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class MenuController : ControllerBase
{
    private readonly IMenuService _menuService;

    public MenuController(IMenuService menuService)
    {
        _menuService = menuService;
    }

    /// <summary>
    /// 获取所有菜单列表
    /// </summary>
    /// <returns>菜单列表</returns>
    [HttpGet]
    public async Task<IActionResult> GetAllMenus()
    {
        var menus = await _menuService.GetAllMenusAsync();
        return Ok(menus);
    }

    /// <summary>
    /// 获取菜单树形结构
    /// </summary>
    /// <returns>菜单树形结构</returns>
    [HttpGet("tree")]
    public async Task<IActionResult> GetMenuTree()
    {
        var menuTree = await _menuService.GetMenuTreeAsync();
        return Ok(menuTree);
    }

    /// <summary>
    /// 根据ID获取菜单详情
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <returns>菜单详情</returns>
    [HttpGet("{menuId}")]
    public async Task<IActionResult> GetMenuById(Guid menuId)
    {
        var result = await _menuService.GetMenuByIdAsync(menuId);
        return Ok(result);
    }

    /// <summary>
    /// 创建新菜单
    /// </summary>
    /// <param name="createMenuDto">菜单创建参数</param>
    /// <returns>创建结果</returns>
    [HttpPost]
    public async Task<IActionResult> CreateMenu([FromBody] CreateMenuDto createMenuDto)
    {
        var result = await _menuService.CreateMenuAsync(createMenuDto);
        return Ok(result);
    }

    /// <summary>
    /// 更新菜单信息
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <param name="updateMenuDto">菜单更新参数</param>
    /// <returns>更新结果</returns>
    [HttpPut("{menuId}")]
    public async Task<IActionResult> UpdateMenu(Guid menuId, [FromBody] UpdateMenuDto updateMenuDto)
    {
        var result = await _menuService.UpdateMenuAsync(menuId, updateMenuDto);
        return Ok(result);
    }

    /// <summary>
    /// 删除菜单（软删除）
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <returns>删除结果</returns>
    [HttpDelete("{menuId}")]
    public async Task<IActionResult> DeleteMenu(Guid menuId)
    {
        var result = await _menuService.DeleteMenuAsync(menuId);
        return Ok(result);
    }

    /// <summary>
    /// 启用菜单
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <returns>操作结果</returns>
    [HttpPatch("{menuId}/enable")]
    public async Task<IActionResult> EnableMenu(Guid menuId)
    {
        var result = await _menuService.EnableMenuAsync(menuId);
        return Ok(result);
    }

    /// <summary>
    /// 禁用菜单
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <returns>操作结果</returns>
    [HttpPatch("{menuId}/disable")]
    public async Task<IActionResult> DisableMenu(Guid menuId)
    {
        var result = await _menuService.DisableMenuAsync(menuId);
        return Ok(result);
    }

    /// <summary>
    /// 获取子菜单列表
    /// </summary>
    /// <param name="parentId">父级菜单ID</param>
    /// <returns>子菜单列表</returns>
    [HttpGet("{parentId}/children")]
    public async Task<IActionResult> GetChildMenus(Guid parentId)
    {
        var childMenus = await _menuService.GetChildMenusAsync(parentId);
        return Ok(childMenus);
    }

    /// <summary>
    /// 移动菜单位置
    /// </summary>
    /// <param name="menuId">菜单ID</param>
    /// <param name="moveRequest">移动参数</param>
    /// <returns>操作结果</returns>
    [HttpPatch("{menuId}/move")]
    public async Task<IActionResult> MoveMenu(Guid menuId, [FromBody] MoveMenuRequest moveRequest)
    {
        var result = await _menuService.MoveMenuAsync(menuId, moveRequest.NewParentId, moveRequest.NewSort);
        return Ok(result);
    }
}

/// <summary>
/// 移动菜单请求模型
/// </summary>
public class MoveMenuRequest
{
    /// <summary>
    /// 新的父级菜单ID
    /// </summary>
    public Guid? NewParentId { get; set; }

    /// <summary>
    /// 新的排序号
    /// </summary>
    public int NewSort { get; set; }
}